// JavaScript Document
var entrust = {
    add: function (data, data2) {
        $.commonAjax({
            url: 'B017',
            type: 'POST',
            data: data,
            isShowError: true,
            success: function (res) {
                data2.code = res.data.code;
                $.cookie('goods_add_code', res.data.code);
                $.cookie('goods_add_data', bp.data_to_str(data2));
                bp.jump('extrust2.html');
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
    }, edit: function (data2) {
        $.commonAjax({
            url: 'B029',
            type: 'POST',
            data: data2,
            isShowError: true,
            success: function (res) {
                $.layermsg(res.msg);
                if (res.code == 200) {
                    $.cookie('goods_add_code', null);
                    $.cookie('goods_add_data', null);

                    setTimeout(function () {
                        bp.jump('auction.html');
                    }, 1500)
                }
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
    }, setimg: function (data2) {
        $.commonAjax({
            url: 'B029',
            type: 'POST',
            data: data2,
            isShowError: true,
            success: function (res) {
                console.log(res);
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
    }, details: function () {
        var get = bp.get_data();
        console.log();
        if (!get.code) {
            return false;
        }
        var data = { auctionCode: get.code }
        //var data = {auctionCode:get.auctionCode};
        $.commonAjax({
            url: 'B035',
            type: 'POST',
            data: data,
            isShowError: true,
            success: function (res) {
                $.cookie('goods_add_data', bp.data_to_str(res.data.goods));
                $.cookie('goods_add_code', res.data.goods.code);
                checkdate();
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
    }, add_img: function (file) {
        var data = { goodsCode: $.cookie('goods_add_code'), file: file }
        $.commonAjax({
            url: 'B033',
            type: 'POST',
            data: data,
            isShowError: true,
            success: function (res) {
                console.log(res)
            }, error: function (res) {
                $.layermsg(res.msg);
            }
        });
    }
}
var img = [];
var video = '';
var checkdate = function () {
    var str = $.cookie('goods_add_data');
    if (str == null) {
        return false;
    }
    var arr = str.split(',');
    var data = {};
    $.each(arr, function (k, v) {
        var a = v.split(':');
        data[a[0]] = a[1];
    })
    if (location.pathname.indexOf('extrust2') > 0) {
        return data;
    } else {
        $.each($('input'), function (k, v) {
            switch (k) {
                case 0: $(v).val(data.name); break;
                case 1: $(v).val(data.quantity); break;
                case 2: $(v).val(data.owner); break;
                case 3: $(v).val(data.factoryTel); break;
                case 4: $(v).val(data.delivery); break;
                case 5: $(v).val(data.introduce); break;
            }
        });
    }
}
$(function () {
    checkdate();
    entrust.details();
    $('.btn1').click(function () {
        var data = {};
        var data2 = {
            code: null,
            quantity: null,
            unit: null,
            categoryId: null,
            factory: null,
            factoryTel: null,
            totalAmount: null,
            owner: null,
            damagedDescribe: null,
            delivery: null,
            introduce: null
        }
        var obj = $('input');
        var data = {};
        data.type = 1;
        $.each(obj, function (k, v) {
            switch (k) {
                case 0: data.name = data2.name = $(v).val(); break;
                case 1: data2.quantity = $(v).val(); break;
                case 2: data2.owner = $(v).val(); break;
                case 3: data2.factoryTel = $(v).val(); break;
                case 4: data2.delivery = $(v).val(); break;
                case 5: data2.introduce = $(v).val(); break;
            }
        });

        if ($.cookie('goods_add_code')) {
            data2.code = $.cookie('goods_add_code');
            $.cookie('goods_add_data', bp.data_to_str(data2));
            bp.jump('extrust2.html');
        } else {
            entrust.add(data, data2);
        }
    });

    $('#file_1').change(function () {
        var fd = new FormData();
        var reader = new FileReader();
        $.each($(this)[0].files, function (k, v) {
            fd.append(k, v);
        });
        $.ajax({
            url: CONFIG.apiUrl + '/upload/imageByH5.api',
            type: 'post',
            data: fd,
            processData: false,
            contentType: false,
            beforeSend: function () {
                if (layer)
                    layer.closeAll('loading');
                $.layerload();
            },
            success: function (res) {
                if (layer)
                    layer.closeAll('loading');
                if (res.code && res.code != 200) {
                    $.layermsg(res.msg);
                    return false;
                }
                var fileList = res.data;
                if (null == fileList || fileList.length == 0) {
                    $.layermsg("文件上传失败");
                    return false;
                }
                htm = '';

                $.each(fileList, function (k, v) {
                    var urlZ = {};
                    var urlZ = fileList[k].largeUrl;
                    img.push({
                        sourceUrl: fileList[k].sourceUrl,
                        largeUrl: urlZ,
                        mediumUrl: fileList[k].mediumUrl,
                        smallUrl: fileList[k].smallUrl,
                    });
                    htm += '<div class="sample-img">'
                    htm += '<img src="' + CONFIG.imgUrl + urlZ + '" alt="">'
                    htm += '</div>'
                });

                $('.upload-img').before(htm);


            },
            error: function () {
                if (layer)
                    layer.closeAll('loading');
                $.layermsg("图片上传异常，请重试");
            }
        })
    });
    $('#video').change(function () {
        var num = $(this).prop('id').split('_')[1];
        var fd = new FormData();
        fd.append('fileType', 'video');
        var obgType = $(this)[0].files[0].type + 'a';
        if (obgType.indexOf("video") < 0) {
            $.layermsg('请上传视频文件!');
            return false;
        }

        var reader = new FileReader();
        reader.readAsDataURL($(this)[0].files[0]);
        fd.append(0, $(this)[0].files[0]);
        $.ajax({
            url: CONFIG.apiUrl + '/upload/documentByH5.api',
            type: 'post',
            data: fd,
            processData: false,
            contentType: false,
            beforeSend: function () {
                if (layer)
                    layer.closeAll('loading');
                $.layerload();
            },
            success: function (res) {
                if (layer)
                    layer.closeAll('loading');
                if (res.code && res.code != 200) {
                    $.layermsg(res.msg);
                    return false;
                }
                var fileList = res.data;
                if (null == fileList || fileList.length == 0) {
                    $.layermsg("文件上传失败");
                    return false;
                }
                var urlZ = fileList[0].path;
                video = urlZ;
                console.log(fileList);
                $('.operation').text('视频已上传');
            },
            error: function () {
                if (layer)
                    layer.closeAll('loading');
                $.layermsg("图片上传异常，请重试");
            }
        })
    });
    $('.subBtn2').click(function () {
        var data = checkdate();
        data.images = JSON.stringify(img);

        console.log(data);
        entrust.edit(data);
    });
})